package com.sf.hackathons.app.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.yoojia.qrcode.qrcode.QRCodeEncoder;
import com.sf.hackathons.app.R;
import com.sf.hackathons.app.utils.AddressPickTask;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.entity.City;
import cn.addapp.pickers.entity.County;
import cn.addapp.pickers.entity.Province;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.SinglePicker;

/**
 * Created by 01367478 on 2017/12/15.
 */

public class TestActivity extends AppCompatActivity {

    Button button;
    TextView textView;
    Button btnOpenScan;
    ImageView imageView;
    Bitmap bitmap;

    Button btnSingle;
    TextView tvSingle;

    Button btnOpen;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        button = (Button) findViewById(R.id.btn_citypicker);
        textView = (TextView) findViewById(R.id.tv_address);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectAddress();
            }
        });
        tvSingle = (TextView) findViewById(R.id.tv_single);
        btnSingle = (Button) findViewById(R.id.btn_single);
        btnSingle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectSingle();
            }
        });

        btnOpenScan = (Button) findViewById(R.id.btn_openscanner);
        btnOpenScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openScan();
            }
        });

        imageView = (ImageView) findViewById(R.id.qr_image);
        Button generate = (Button) findViewById(R.id.btn_generate);
        generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                generateCode();
            }
        });

        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                saveBitmap(bitmap);
                return true;
            }
        });

        btnOpen = (Button) findViewById(R.id.btn_dialog);
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TestActivity.this, ShareDialogActivity.class);
                startActivity(intent);
            }
        });
    }

    private void generateCode() {
        Bitmap centerImage = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_logo);
        Bitmap qrCodeImage = new QRCodeEncoder.Builder()
                .width(300) // 二维码图案的宽度
                .height(300)
                .paddingPx(0) // 二维码的内边距
                .marginPt(3) // 二维码的外边距
                .centerImage(centerImage) // 二维码中间图标
                .build()
                .encode("hello world");

        imageView.setImageBitmap(qrCodeImage);

        this.bitmap = qrCodeImage;
    }

    private void openScan() {
        Intent intent = new Intent(this, WeChatCaptureActivity.class);
        startActivity(intent);
    }

    private void selectAddress() {
        AddressPickTask task = new AddressPickTask(this);
        task.setHideProvince(false);
        task.setHideCounty(false);

        task.setCallback(new AddressPickTask.Callback() {
            @Override
            public void onAddressInitFailed() {
                showToast("数据初始化失败");
            }

            @Override
            public void onAddressPicked(Province province, City city, County county) {
                textView.setText(province.getAreaName() + city.getAreaName() + county.getAreaName());
            }
        });
        task.execute("广东", "深圳", "南山");
    }

    private void selectSingle() {
        SinglePicker<String> picker = new SinglePicker<>(this,
                new String[]{
                        "0~1kg", "1~5kg", "5~15kg", "15~50kg", ">=50kg"
                });
        picker.setCanLoop(false);
        picker.setTopBackgroundColor(0xFFEEEEEE);
        picker.setTopHeight(50);
        picker.setTopLineColor(0xFF33B5E5);
        picker.setTopLineHeight(1);
        picker.setTitleText("请选择");
        picker.setTitleTextColor(0xFF999999);
        picker.setTitleTextSize(12);
        picker.setCancelTextColor(0xFF33B5E5);
        picker.setCancelTextSize(13);
        picker.setSubmitTextColor(0xFF33B5E5);
        picker.setSubmitTextSize(13);
        picker.setSelectedTextColor(0xFFEE0000);
        picker.setUnSelectedTextColor(0xFF999999);
        LineConfig config = new LineConfig();
        config.setColor(0xFFEE0000);//线颜色
        config.setAlpha(140);//线透明度
        config.setRatio((float) (1.0 / 8.0));//线比率
        picker.setLineConfig(config);
        picker.setItemWidth(200);
        picker.setBackgroundColor(0xFFE1E1E1);
        picker.setSelectedIndex(0);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                tvSingle.setText(item);
            }
        });
        picker.show();

    }

    public void saveBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return;
        }
        String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        File f = new File(filePath, System.currentTimeMillis() + ".png");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Toast.makeText(this, "图片已保存", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
